package com.beverages.commodities.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beverages.commodities.client.SharedFilesClient;
import com.beverages.commodities.entity.Commodities;
import com.beverages.commodities.entity.dto.CommoditiesDTO;
import com.beverages.commodities.mapper.CommoditiesMapper;
import com.beverages.commodities.service.ICommoditiesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommoditiesImpl extends ServiceImpl<CommoditiesMapper, Commodities> implements ICommoditiesService {

    @Autowired
    private SharedFilesClient sharedFilesClient;

    @Override
    public CommoditiesDTO convertCommoditiesEntityToCommoditiesDTO(Commodities commodities){
        CommoditiesDTO dto = new CommoditiesDTO();
        BeanUtils.copyProperties(commodities, dto);

        // 调用 shared-file-service 获取图片 URL
        String imageUrl = sharedFilesClient.getFileUrlById(commodities.getImageId()).getData();
        dto.setImageUrl(imageUrl);

        // 将 ingredients 字符串转换为 List<String>
//            if (commodities.getIngredients() != null) {
//                String[] ingredientsArray = commodities.getIngredients().split(",");
//                List<String> ingredientsList = Arrays.asList(ingredientsArray); // 转为字符串列表
//                dto.setIngredients(ingredientsList);
//            }
        // 定义映射关系
        Map<String, String> ingredientMap = new HashMap<>();
        ingredientMap.put("1", "椰汁");
        ingredientMap.put("2", "牛奶");
        ingredientMap.put("3", "气泡水");
        ingredientMap.put("4", "可乐");
        ingredientMap.put("5", "橙汁");

        if (commodities.getIngredients() != null) {
            String[] ingredientsArray = commodities.getIngredients().split(",");

            // 根据映射关系将数字转换为对应的名称
            List<String> ingredientsList = Arrays.stream(ingredientsArray)
                    .map(ingredientMap::get)
                    .collect(Collectors.toList());

            dto.setIngredients(ingredientsList);
        }

        // 将 type 转化为字符串
        String typeString;
        switch (commodities.getType()) {
            case 1:
                typeString = "碳酸饮料";
                break;
            case 2:
                typeString = "奶茶";
                break;
            case 3:
                typeString = "果茶";
                break;
            case 4:
                typeString = "圣代";
                break;
            default:
                typeString = "未知类型";
                break;
        }
        dto.setType(typeString);

        return dto;
    }
}
